<?php

class Country_Model_DbTable_Polls extends Engine_Db_Table
{
    protected $_rowClass = 'Country_Model_Poll';
    
    
    
    public function removePoll($poll_id){
        $this->delete(array(
            'poll_id = ?'=>$poll_id
        ));
        
    }
    public function getOptions($poll_id){
        $pollOptios = Engine_Api::_()->getDbtable('polloptions','country');
        return $pollOptios->getPollOptions($poll_id);
        
    }
    public function getVote($user_id,$option_id,$country_id){
        $voteTable = Engine_Api::_()->getDbtable('pollvotes','country');
        $select = $voteTable->select()
                            ->where('country_id = ?',$country_id)
                            ->where('polloption_id =?',$option_id)
                            ->where('user_id = ?',$user_id);
        $row = $voteTable->fetchRow($select);
        return $row;
    }
    
    
    
    public function hasVote($user_id,$option_id,$country_id){
        $row = $this->getVote($user_id,$option_id,$country_id);
        if (!empty($row)) return true;
        return false;
    }
    
    public function setVote($user_id,$option_id,$country_id){
        $voteTable = Engine_Api::_()->getDbtable('pollvotes','country');
        $row = $voteTable->createRow();
        $row->user_id = $user_id;
        $row->country_id = $country_id;
        $row->polloption_id = $option_id;
        $row->save();
        
        $pollOption = Engine_Api::_()->getItem('polloption',$option_id);
        if (!empty($pollOption)) {
            $pollOption->votecount = $pollOption->votecount + 1;
            $pollOption->save();
        }
    }
  
    
}
